package com.micro.balance;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import org.springframework.stereotype.Component;

@Component("randomweight")
public class RandomWeightLoadBalance extends AbstractLoadBalance{

	@Override
	protected String doSelect(Map<String, Integer> map) {
		ArrayList<String> lists = new ArrayList<String>();
		
		//根据权重来生成比重的数据放入List集合
		Iterator<String> iterator = map.keySet().iterator();
		while(iterator.hasNext()){
			String key=iterator.next();
			Integer weight=map.get(key);
			
			for(int i=0;i<weight;i++){
				lists.add(key);
			}
		}
		//开始随机
		int len=lists.size();
        Random random=new Random();
        int index=random.nextInt(len);
        
        return lists.get(index);
	}

}
